package com.demo.bean;

import com.demo.callback.ServerStateCallback;
import com.demo.config.WkConfig;
import com.demo.tool.ServerTool;
import com.demo.tool.WkFileTool;
import com.demo.tool.WkHtmlTool;
import com.demo.tool.WkLog;

import java.io.File;
import java.util.List;
import java.util.Map;
import org.apache.commons.fileupload.FileUploadBase;
import fi.iki.elonen.NanoHTTPD;


public class WkHttpClient extends NanoHTTPD {

	private List<File> rootDirs;
	private WkHtmlTool wkHtmlTool = new WkHtmlTool() ;
	private ServerTool serverTool = new ServerTool();
	private WkFileTool wkFileTool = new WkFileTool() ;
	private ServerStateCallback serverStateCallback ;
	public WkHttpClient(int port, List<File> rootDir  , ServerStateCallback serverStateCallback) {
		super(port);
		this.rootDirs = rootDir;
		this.serverStateCallback = serverStateCallback;
		wkFileTool.setServerStateCallback(serverStateCallback);
	}



	/**
	 * 入口
	 *String userIp =  session.getRemoteIpAddress() ; // 客户端ip
	 *
	 * public final static String APPAPI = "appApi";
	 * 请求中包含了 appApi 关键字的就认为是 给APP的接口。 否则认为是文件管理请求。
	 */
	@Override
	public Response serve(IHTTPSession session) {
		Map<String, String> parms = session.getParms();
		String ip = session.getRemoteIpAddress() ;
		String uri = session.getUri();
		Method method =session.getMethod() ;
		if(serverStateCallback!=null){
			serverStateCallback.oherInfo(ip+ "  正在请求="+uri+"  "+method);
		}
		NanoHTTPD.Response response ;
			if (method== Method.POST ) {
				response =processPostRequest(session ,parms) ;
			} else if ( method == Method.GET ) {
				response =processGetRequest(session ,uri,parms) ;
			}else{
				response= wkFileTool.doshowFile(   session ,  serverTool ,  rootDirs,  wkHtmlTool);
			}
		return response ;
	}


	private NanoHTTPD.Response processPostRequest(IHTTPSession session ,Map<String, String> parms){
		NanoHTTPD.Response response ;
		boolean isFileUplowd = FileUploadBase.isMultipartContent( new NanoHttpdContext(session)) ;
		if( isFileUplowd ){
			// post请求，上传文件
			try {
				WkLog.showLog("接收到 文件上传的post请求");
				response= wkFileTool.fileUpload(   session ,  serverTool ,  rootDirs,  wkHtmlTool     );
			} catch ( Exception e) {
				response=serverTool.renderApiMsg("服务器内部异常1 。"+e.getMessage()) ;
				e.printStackTrace();
			}
		}else{
			WkLog.showLog("接收到普通的post请求");
			response= serverTool.renderApiMsg("接收到普通的post请求。"+System.currentTimeMillis() ) ;
		}
		return  response ;
	}


	private NanoHTTPD.Response processGetRequest(IHTTPSession session ,String uri ,Map<String, String> parms){
		NanoHTTPD.Response response ;
		if(parms.keySet().size() > 0){

			if(uri.contains(WkConfig.APPAPI)){
				response= serverTool.renderApiMsg("这是返回给api接口的测试数据 有参数的。GET 请求"+System.currentTimeMillis()) ;
			}else{
				if (parms.containsKey(WkConfig.DELETE)) {
					try {
						response = wkFileTool.deal_delFile(  session, parms ,rootDirs ,serverTool,wkHtmlTool);
					} catch (Exception e) {
						response=serverTool.renderApiMsg("服务器内部异常2 。"+e.getMessage()) ;
						e.printStackTrace();
					}
				} else if (parms.containsKey(WkConfig.MKDIR)) {
					response= wkFileTool.makeDir( session, parms ,rootDirs ,serverTool ,wkHtmlTool);
				}else{
					response=serverTool.renderApiMsg("不存在该api 。"+System.currentTimeMillis()) ;
				}
			}
		}else{

			//  appApi
			if(uri.contains(WkConfig.APPAPI)){
				response= serverTool.renderApiMsg("这是返回给api接口的测试数据，无参数的。"+System.currentTimeMillis()) ;
			}else{
				WkLog.showLog("------"+session.getUri());
				response= wkFileTool.doshowFile(   session ,  serverTool ,  rootDirs,  wkHtmlTool);
			}

		}
		return response ;
	}

}
